import axios from 'axios'
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
import { ElMessage } from "element-plus";
export const getRequest = (params?: AxiosRequestConfig): AxiosInstance => {

    const server: AxiosInstance = axios.create({
        timeout: 30000,
        method: 'GET',
        baseURL: import.meta.env.VITE_BASE_URL,
        headers: {
            Accept: "application/json",
        },
        ...params
    })
    //使用请求拦截
    server.interceptors.request.use(
        function (config: any) {
            return config
        },
        function (error: any) {
            return Promise.reject(error)
        }
    )
    //使用返回拦截
    server.interceptors.response.use(
        function (response: any) {
            const { status } = response;

            if (status === 200) {
                return response.data
            } else {
                console.error("请求回来的数据有异常！", response.data)
            }
        },
        function (error: any) {
            ElMessage.error(error.message)
            return Promise.reject(error)
        }
    )
    return server
}